package cn.com.twoke.game.client.handler;

import cn.com.twoke.game.client.cache.ClientCacheManager;
import cn.com.twoke.game.client.games.framework.core.Game;
import cn.com.twoke.game.client.ui.UiManager;
import cn.com.twoke.game.common.entity.message.StopGameMessage;
import cn.com.twoke.game.common.handler.MessageHandler;

import javax.swing.*;
import java.io.IOException;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;

public class StopGameMessageHandler implements MessageHandler<StopGameMessage> {

    @Override
    public void handle(Selector selector, SocketChannel channel, StopGameMessage message) throws IOException {
        Game game = ClientCacheManager.INSTANCE.getGame(message.getGameId());
        if (null != game) {
            if (!message.getUserId().equals(game.getPlayerName())) {
                JOptionPane.showMessageDialog(game.getWindow(), "对方已退出游戏", "提示", JOptionPane.INFORMATION_MESSAGE);
            }
            // 1、清除缓存
            ClientCacheManager.INSTANCE.removeGame(message.getGameId());
            // 2、关闭窗口
            game.getWindow().dispose();
            // 3、打开主界面
            UiManager.WINDOW.toHome();
        }

    }


}
